package com.xuesi.demoadmin.aspect;

import javax.servlet.http.HttpServletRequest;

import com.xuesi.demoadmin.service.impl.TokenServiceImpl;
import com.xuesi.demoadmin.service.impl.UserServiceImpl;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.xuesi.demoadmin.entity.R;
import com.xuesi.demoadmin.entity.User;

@Aspect
@Component
public class AuthAspect {
    @Pointcut("@annotation(com.xuesi.demoadmin.annotation.Auth)")
    public void isLogin() {
    }

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private TokenServiceImpl tokenServiceImpl;

    @Around("isLogin()")
    public Object doAroundConversion(ProceedingJoinPoint pjp) throws Throwable {
        String token = this.request.getHeader("token");

        if (token == null || token.equals("")) {
            return new R<Object>(401, "请先登录");
        }

        User user = this.tokenServiceImpl.decode(token);
        if (user == null) {
            return new R<Object>(401, "请先登录");
        }

        Object res = pjp.proceed();
        return res;
    }
}
